package com.example.dessertstoressm.controller;


import com.example.dessertstoressm.Service.CatalogService;
import com.example.dessertstoressm.domain.Category;
import com.example.dessertstoressm.domain.Item;
import com.example.dessertstoressm.domain.Product;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;


@Controller
public class productAutoController {

    @Autowired
    public CatalogService catalogService;

    @RequestMapping("viewProduct")
    public String viewProductBySearchAuto(HttpServletRequest req){
        String productId = req.getParameter("productId");

        Product product = catalogService.getProduct(productId);
        List<Item> itemList = catalogService.getItemListByProduct(productId);

        //根据product得到搜索结果的类，放在return to的a标签里
        String categoryId = product.getCategoryId();
        Category category = catalogService.getCategory(categoryId);

        HttpSession session = req.getSession();
        session.setAttribute("product",product);
        session.setAttribute("itemList",itemList);
        session.setAttribute("category" ,category );

        return "catalog/product";
    }
}
